package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.mapper.UserMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

/**
 * @Author：XueShiHao
 * @Date：2025/6/20 星期五 14:13:17
 * @Email:747278007@qq.com
 * @Description:
 **/
@Api("用户端的接口")
@Slf4j
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

        @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
            log.info("登录用户的信息：{}",userLoginDTO);
            //调用微信登录
          User user= userService.wxLogin(userLoginDTO);
            //为微信登录用户生成token
            HashMap<String, Object> claims=new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID,user.getId());
            String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
            UserLoginVO userLoginVO= UserLoginVO.builder()
                    .id(user.getId())
                            .openid(user.getOpenid())
                                    .token(token)
                    . build();

        /**
         * 需要返回用户id，用户的openId，以及用户的令牌token
         */
        return Result.success(userLoginVO);
    }
}
